package logic

import (
	"cart/cartrpc/cartrpc"
	"cart/cartrpc/internal/svc"
	"cart/model/cache"
	"cart/model/mysqls"
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
)

type ShopAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShopAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShopAddLogic {
	return &ShopAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ShopAddLogic) ShopAdd(in *cartrpc.ShopAddRequest) (*cartrpc.ShopAddResponse, error) {
	// todo: add your logic here and delete this line

	shop := mysqls.Shop{
		ShopName: in.ShopName,
		Price:    float64(in.Price),
		Num:      int(in.Num),
		Image:    in.Image,
		Status:   in.Status,
	}
	err := shop.ShopAdd()
	if err != nil {
		return nil, fmt.Errorf("商品添加失败")
	}

	cache.ShopVal(int(shop.ID), int(in.Num))

	return &cartrpc.ShopAddResponse{
		ShopId: int64(shop.ID),
	}, nil
}
